#!/usr/bin/env bash

# daily-update
# This program is part of the backup-data program
# backup-data is a component of the faridayix robot operating system
# Copyright (C) 2009 Scott C. MacCallum
# scottmaccal@gmail.com
# http://code.google.com/p/backup-data/

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# robot: is a variable holding the hostname

# printf: print function "output this string of characters to the screen"

# mkdir: make directory -v verbose -p create all directories leading
# up to the given directory that do not exist already. If the given
# directory already exists, ignore the error.

# touch: create file

# sh: shell execute script

# cat: concatenate file1 >> append to file2

# rsync: remote synchronize -v verbose -a archive

# rm: remove -v verbose -r recursive -f force

robot=`hostname`

# -----------------------------------------------------
# Make a temporary, backup-data, daily-update directory
# -----------------------------------------------------

printf "[ $robot ] I am going to make a temporary, backup-data, daily-update directory in my non-volatile storage.\n\n"

mkdir -v -p /tmp/backup-data/daily-update

printf "\n"

printf "[ $robot ] I am finished with this task $USER.\n\n"

# ------------------------------------------------------------
# Make a temporary, backup-data, daily-update, update-log file
# ------------------------------------------------------------

printf "[ $robot ] I am going to make a temporary, backup-data, daily-update, update-log file in my non-volatile storage.\n\n"

touch /tmp/backup-data/daily-update/update-log

printf "[ $robot ] I am finished with this task $USER.\n\n"

# --------------------------------------------------
# Execute the user, local, binary, daily-update file
# --------------------------------------------------

printf "[ $robot ] I am going to execute user, local, binary, daily-update file and write the output to screen and the tee-output file.\n\n"

sh /usr/local/bin/daily-update | tee -a /tmp/backup-data/daily-update/tee-output

printf "[ $robot ] I am finished with this task $USER.\n\n"

# ------------------------------------------------
# Append the tee-ouput file to the update-log file
# ------------------------------------------------

printf "[ $robot ] I am going to append the data in the temporary, backup-data, daily-update, tee-output file to the temporary, backup-data, daily-update, update-log file.\n\n"

cat /tmp/backup-data/daily-update/tee-output >> /tmp/backup-data/daily-update/update-log

printf "[ $robot ] I am finished with this task $USER.\n\n"

# ----------------------------------------------------------------------------------
# Update the temporary, backup-data, daily-update, update-log file on the server
# ----------------------------------------------------------------------------------

printf "[ $robot ] I am going to update the temporary, backup our data, daily-update, update-log file on the remote robot your specifide.\n\n"

rsync -v -a /tmp/backup-data/daily-update/update-log backup@ipaddress:/usr/home/backup/$robot/daily-update/update-log

printf "[ $robot ] I am finished with this task $USER.\n\n"

# ----------------------------------------------------------
# Remove the temporary, backup our data, directory and files
# ----------------------------------------------------------

printf "Removing the temporary, backup our data, directory and files from my non-volatile storage.\n\n"

rm -v -r -f /tmp/backup-data

printf "[ $robot ] I am finished with this task $USER.\n\n"

# ----------------
# Exit the program
# ----------------

exit                                                            
# ---End of File---
